package homework_4_minesweeper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SouthMenu extends JPanel {
    private static JTextField times=new JTextField();
    private static JTextField mines=new JTextField();
    private JPanel blank=new JPanel();
    private static int time;
    private static int mine;
    private static Timer timer;
    private static Color win=new Color(85, 149, 78);
    private static Color start=new Color(108,108,108);
    private static Color lose=new Color(136, 56, 56);
    public SouthMenu(){
        this.setLayout(new BorderLayout());
        this.add(times,BorderLayout.WEST);
        this.add(mines,BorderLayout.EAST);
        this.add(blank,BorderLayout.CENTER);
        times.setBackground(start);//灰色背景
        mines.setBackground(start);
        times.setForeground(Color.BLACK);                       //黑色字（暂定）
        mines.setForeground(Color.BLACK);
        times.setEditable(false);                               //不能修改文本框里的数据
        mines.setEditable(false);
        times.setHorizontalAlignment(JTextField.CENTER);        //居中
        mines.setHorizontalAlignment(JTextField.CENTER);
        times.setPreferredSize(new Dimension(100,50));
        mines.setPreferredSize(new Dimension(100,50));
    }

    public static void setMine(int mine){
        SouthMenu.mine=mine;
        mines.setText(""+mine);
    }
    public static void setTime(int time) {
        SouthMenu.time=time;
        times.setText(""+time);
    }
    public static void increaseTime(){
        if(time<999) {
            time += 1;
        }
        setTime(time);
    }
    public static void decreaseMine(){
        mine--;
        mines.setText(""+mine);
        if(mine<0){
            mines.setForeground(Color.RED);
        }
    }
    public static void increaseMine(){
        mine++;
        mines.setText(""+mine);
        if(mine>=0){
            mines.setForeground(Color.BLACK);
        }
    }

    public static void lose(){
        times.setBackground(lose);
        mines.setBackground(lose);
        times.setText("LOSE");
        mines.setText("LOSE");
        stop();
    }
    public static void win(){
        times.setBackground(win);
        mines.setBackground(win);
        mines.setText("WIN");
        stop();
    }
    public static void start(){
        if(timer!=null) {
            timer.stop();
        }
        time=0;
        setTime(0);
        times.setBackground(start);
        mines.setBackground(start);
        timer=new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                increaseTime();
            }
        });
        timer.start();
    }
    public static void stop() {
        if (timer!= null && timer.isRunning()) {
            timer.stop();
        }
    }
}
